package com.atguigu.ucenter.member.controller;


import com.atguigu.utils.JwtUtils;
import com.atguigu.utils.R;
import com.atguigu.ucenter.member.entity.Member;
import com.atguigu.ucenter.member.entity.vo.MemberQuery;
import com.atguigu.ucenter.member.service.MemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author admin
 * @since 2020-07-12
 */
@Api(value = "ucenter-member", description = "用户登录注册接口")
@CrossOrigin
@RestController
@RequestMapping("/ucenter/member")
public class MemberController {

    @Resource
    private MemberService memberService;

    @ApiOperation("登录接口-参数memberQuery")
    @PostMapping("/login")
    public R memberLogin(@RequestBody MemberQuery memberQuery) {
        String token = memberService.login(memberQuery.getMobile(), memberQuery.getPassword());
        return R.ok().data("token", token);
    }

    @ApiOperation("注册接口-参数memberQuery")
    @PostMapping("/register")
    public R memberRegister(@RequestBody MemberQuery memberQuery) {
        boolean b = memberService.register(memberQuery);
        if (b) {
            return R.ok();
        }
        return R.error().message("注册失败!");
    }

    @ApiOperation("获取用户信息")
    @GetMapping("/getMember")
    public R getMemberInfo(HttpServletRequest request) {
        String tokenId = JwtUtils.getToken(request);
        if (StringUtils.isEmpty(tokenId)) {
            return R.error();
        }
        Member member = memberService.getMemberByToken(tokenId);
        return R.ok().data("member", member);
    }

}

